package cn.xinfei.xdecision.engine.job;

import cn.xinfei.xdecision.model.CacheManagerRequest;
import cn.xinfei.xdecision.service.MetadataCacheService;
import cn.xinfei.xdecision.thread.CustomerThreadPoolExecutor;
import cn.xinfei.xdecision.thread.ThreadPoolExecutorConfig;
import cn.xinfei.xdecision.thread.monitor.ThreadPoolRegister;
import com.alibaba.fastjson.JSONObject;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


@Slf4j
@Service
public class MetadataInitJob {


    @Autowired
    private MetadataCacheService metadataCacheService;

    @XxlJob(value = "metadataInitJob", init = "init", destroy = "destroy")
    public ReturnT<String> metadataInitJob(String param) {
        log.info("metadataInitJob:{}", param);
        CacheManagerRequest cacheManagerRequest = null;
        if (StringUtils.isNotEmpty(param)) {
            cacheManagerRequest = JSONObject.parseObject(param, CacheManagerRequest.class);
        } else {
            cacheManagerRequest = new CacheManagerRequest();
        }
        long start = System.currentTimeMillis();
        try {
            metadataCacheService.reload(cacheManagerRequest);
            long end = System.currentTimeMillis();
            log.info("============ metadataInitJob 调用耗时：{},request={}", (end - start), JSONObject.toJSONString(cacheManagerRequest));
        } catch (Exception e) {
            log.error("缓存接口异常，error={},apiRequest={}", e.getMessage(), JSONObject.toJSONString(cacheManagerRequest), e);
        } finally {

        }
        XxlJobLogger.log("metadataInitJob success,cacheManagerRequest="+JSONObject.toJSONString(cacheManagerRequest));

        return ReturnT.SUCCESS;
    }


    @XxlJob(value = "threadRefreshJob", init = "init", destroy = "destroy")
    public ReturnT<String> threadRefreshJob(String param) {
        log.info("threadRefreshJob:{}", param);
        long start = System.currentTimeMillis();
        try {
            Map<String, CustomerThreadPoolExecutor> customerThreadPoolExecutorMap = CustomerThreadPoolExecutor.customerThreadPoolExecutorMap;
            Map<String, ThreadPoolExecutorConfig> threadPoolExecutorConfigMap = CustomerThreadPoolExecutor.threadPoolExecutorConfigMap;
            log.debug("customerThreadPoolExecutorList size={}", customerThreadPoolExecutorMap.size());
            customerThreadPoolExecutorMap.entrySet().forEach(e -> {
                String key = e.getKey();
                CustomerThreadPoolExecutor threadPoolExecutor = e.getValue();
                ThreadPoolRegister.checkParamChange(threadPoolExecutorConfigMap.get(key), threadPoolExecutor);
            });
            long end = System.currentTimeMillis();
            log.info("============ threadRefreshJob 调用耗时：{}", (end - start));
        } catch (Exception e) {
            log.error("缓存接口异常，error={},apiRequest={}", e.getMessage(), param, e);
        }
        XxlJobLogger.log("threadRefreshJob success,param="+param);
        return ReturnT.SUCCESS;
    }


    public void init() {
        log.info("init");
    }

    public void destroy() {
        log.info("destory");
    }
}
